package com.yijiu.trb;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.LoginResponse;
import com.yijiu.response.RequestAccountInfoResponse;
import com.yijiu.response.RequestPersonalInfoResponse;

/**
 * 登录页面
 * 
 * @author TangChao_Lenovo
 */
public class LoginActivity extends BTitleActivity implements OnClickListener
{
	/**
	 * 标签
	 */
	private String Tag = null;

	/**
	 * 帐户名
	 */
	private String accountName;

	/**
	 * 登陆成功后返回的appKey
	 */
	private String appKey = "";

	/**
	 * 登录密码
	 */
	private String loginPassword;

	/**
	 * 登录、注册、忘记密码、用户名、密码
	 */
	private TextView loginButton, registerButton, forgetPasswordTextView, accountNameEditText, passwordEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.login_layout);
		initWidget();
		Intent intent = getIntent();
		Tag = intent.getStringExtra("TAG");
	}

	/**
	 * 初始化控件 、绑定监听器
	 */
	protected void initWidget()
	{
		super.initWidget();
		titleView.setText("登录");
		loginButton = (TextView) findViewById(R.id.login);
		registerButton = (TextView) findViewById(R.id.register);
		forgetPasswordTextView = (TextView) findViewById(R.id.forget_password);
		accountNameEditText = (TextView) findViewById(R.id.account_number);
		passwordEditText = (TextView) findViewById(R.id.password);
		loginButton.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		forgetPasswordTextView.setOnClickListener(this);
		/** 调用Html属性添加下划线 */
		forgetPasswordTextView.setText(Html.fromHtml("<u>" + "忘记密码" + "</u>" + "?"));
		forgetPasswordTextView.setTextColor(getResources().getColor(R.color.red));
	}

	/**
	 * 登录请求
	 */
	private void requestLogin()
	{
		accountName = accountNameEditText.getText().toString();
		loginPassword = passwordEditText.getText().toString();
		// 判断用户是否输入
		if (StringUtils.isNull(accountName))
		{
			app.showMsg("帐户名不能为空");
			return;
		}
		if (StringUtils.isNull(loginPassword))
		{
			app.showMsg("登录密码不能为空");
			return;
		}
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=doLogin&userName=" + accountName + "&password=" + loginPassword;
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction action = new BaseAction(encodedData, encriptData);
		// 登录响应
		LoginResponse loginResponse = new LoginResponse();
		// 请求服务器
		Request request = new Request(action, loginResponse, ActionType.LOGIN_CODE);
		netManager.excute(request, this, this);
		showProgress("正在登录，请稍候");
	}

	/**
	 * 个人信息请求
	 * 
	 * @param appKey
	 */
	private void requestPersonInfo(String appKey)
	{
		String inputStr = "service=personalInfoService";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestPersonalInfoResponse requestPersonalInfo = new RequestPersonalInfoResponse();
		Request request = new Request(action, requestPersonalInfo, ActionType.REQUEST_PERSONAL_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取个人信息");
	}

	/**
	 * 账户信息请求
	 * 
	 * @param appKey
	 */
	private void requestAccountInfo(String appKey)
	{
		String inputStr = "service=queryAcountInfo";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestAccountInfoResponse requestAccountInfoResponse = new RequestAccountInfoResponse();
		Request request = new Request(action, requestAccountInfoResponse, ActionType.REQUEST_ACCOUNT_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取账户信息");
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.LOGIN_CODE:
				LoginResponse loginResponse = (LoginResponse) r.getResponse();
				appKey = loginResponse.field.appKey;
				// 将登陆后服务器返回的appKey缓存进数据库
				CacheData.APP_KEY = appKey;
				if (appKey.equals(""))
				{
					appKey = loginResponse.field.appKey;
				}
				if (!appKey.equals(""))
				{
					// 登陆成功后请求个人数据
					requestPersonInfo(appKey);
				}
				break;
			case ActionType.REQUEST_PERSONAL_INFO:// 请求个人信息
				RequestPersonalInfoResponse requestPersonalInfoResponse = (RequestPersonalInfoResponse) r.getResponse();
				CacheData.USER_NAME = requestPersonalInfoResponse.field.userName;
				CacheData.REAL_NAME = requestPersonalInfoResponse.field.realName;
				CacheData.MOBILE_NUMBER = requestPersonalInfoResponse.field.mobile;
				CacheData.EMAIL = requestPersonalInfoResponse.field.email;
				CacheData.BANK_ICON_URL = requestPersonalInfoResponse.field.banklog;
				CacheData.BANK_PEOPLE_NAME = requestPersonalInfoResponse.field.bankOpenName;
				CacheData.BANK_CARD_NUMBER = requestPersonalInfoResponse.field.bankCardNo;
				CacheData.BANK_CARD_NAME = requestPersonalInfoResponse.field.bankName;
				CacheData.ROLE = requestPersonalInfoResponse.field.role + "";
				CacheData.REAL_NAME_STATUS = requestPersonalInfoResponse.field.realNameStatus;
				CacheData.CERT_NUM = requestPersonalInfoResponse.field.certNo;
				CacheData.CARD_OFF = requestPersonalInfoResponse.field.cardOff;
				CacheData.MEMBER_NO = requestPersonalInfoResponse.field.memberNo;
				CacheData.HOST = requestPersonalInfoResponse.field.host;
				CacheData.CERT_FRONT_PATH = requestPersonalInfoResponse.field.certFrontPath;
				CacheData.CERT_BACK_PATH = requestPersonalInfoResponse.field.certBackPath;
				if (!appKey.equals(""))
				{
					// 请求个人信息成功后,请求账户信息
					requestAccountInfo(appKey);
				}
				break;
			case ActionType.REQUEST_ACCOUNT_INFO:// 请求账户信息
				RequestAccountInfoResponse requestAccountInfoResponse = (RequestAccountInfoResponse) r.getResponse();
				CacheData.AVAILABLE_BALANCE = requestAccountInfoResponse.field.availableBalance.toString();
				CacheData.CERTIFY_STATUS = requestAccountInfoResponse.field.certifyStatus;
				CacheData.LOGIN_STATUS = true;
				Intent intent = new Intent(this, MainTabActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				finish();
				SuggestProjectActivity.instance.finish();
				break;
		}
	}

	/**
	 * 操作失败返回的函数
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.LOGIN_CODE:
				app.showMsg(errorMsg);
				CacheData.LOGIN_STATUS = false;
				break;
			case ActionType.REQUEST_PERSONAL_INFO:
				app.showMsg("获取个人信息失败！");
				break;
			case ActionType.REQUEST_ACCOUNT_INFO:
				app.showMsg("获取账户信息失败！");
				break;
		}
	}

	/**
	 * 重复提交返回的函数
	 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.login:// 登录
				requestLogin();
				break;
			case R.id.register:// 注册
				initIntent(this, RegistActivity.class);
				break;
			case R.id.forget_password:// 忘记密码
				initIntent(this, ForgetPwdActivity.class);
				break;
			default:
				break;
		}
	}
}
